/**
 * @program: pettyloan
 * @ClassName UpdatRepayState
 * @author: 汪江
 * @create: 2022-12-10 17:17
 **/
package com.wn.pettyloan.quartz;


import com.wn.pettyloan.entity.Overdue;
import com.wn.pettyloan.entity.RepayRecord;
import com.wn.pettyloan.po.OverdueInfo;
import com.wn.pettyloan.service.OverdueService;
import com.wn.pettyloan.service.RepayRecordService;
import com.wn.pettyloan.util.OverDueRepay;
import com.wn.pettyloan.util.utilentity.OverDue;
import com.wn.pettyloan.util.utilentity.RePay;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Slf4j
public class UpdatRepayState extends QuartzJobBean {
    @Resource
    private RepayRecordService repayRecordService;
    @Resource
    private OverdueService overdueService;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        List<RepayRecord> repayRecords = repayRecordService.inspectStatus();
        //System.out.println(repayRecords.toString());
        List<Integer> list = new LinkedList<Integer>();
        for (int i = 0; i < repayRecords.size(); i++) {
            long overdueTime = repayRecords.get(i).getOverdueTime().getTime();
            long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
            if (now > overdueTime) {
                Integer repayId = repayRecords.get(i).getRepayRecordId();
                list.add(repayId);
            }
        }
        list.add(0);
        System.out.println(list.toString());
        int i = repayRecordService.updateStatus(list);
        if (i > 0) {
            log.info("今天还款记录状态修改成功" + LocalDateTime.now());
        } else {
            log.info("今天还款记录状态修改失败" + LocalDateTime.now());
        }
        //更新逾期表
        List<Overdue> overdues = overdueService.selAllOverdue();
        for (Overdue oo : overdues) {
//            long overdueTime = oo.getOverdueTime().getTime();
//            long now = new Date().getTime();
//            long overTime = (now - overdueTime) / 60 / 60 / 24+1;
//            Integer integer = (int) overTime;
            OverDue overDue1=new OverDue();
            overDue1.setOverDueDate(1);
            overDue1.setTotalOverdueMoney(oo.getTotalOverdueMoney().doubleValue());
            overDue1.setSurplusPrincipal(Double.valueOf(oo.getBackup1()));
            overDue1.setOverDueInterest(Double.valueOf(oo.getBackup2()));
            RePay rePay = OverDueRepay.overDueMethod(overDue1);
            oo.setOverduePrincipal(new BigDecimal(rePay.getPrincipal()));
            oo.setOverdueInterest(new BigDecimal(rePay.getInterest()));
            oo.setTotalOverdueMoney(new BigDecimal(rePay.getTotal()));
            int i1 = overdueService.updateByPrimaryKeySelective(oo);
            System.out.println(i1);
        }
        //第一次添加逾期表
        List<OverdueInfo> overdueInfos = repayRecordService.overdueInfo(list);
        for (OverdueInfo o : overdueInfos) {
            //计算逾期的应还利息
            OverDue overDue = new OverDue();
            overDue.setOverDueDate(1);
            overDue.setTotalOverdueMoney(o.getCurrentTotalMoney().doubleValue());
            BigDecimal repayShould = o.getRepayShould();
            BigDecimal repayAlready = o.getRepayAlready();
            overDue.setSurplusPrincipal(repayShould.doubleValue() - repayAlready.doubleValue());
            overDue.setOverDueInterest(o.getOverdueInterest().doubleValue());
            RePay rePay = OverDueRepay.overDueMethod(overDue);
            //设置逾期表参数
            //逾期记录表
            Overdue overdue = new Overdue();
            overdue.setRepayRecordId(o.getRepayRecordId());
            overdue.setOverduePrincipal(o.getCurrentTotalMoney());
            String s = String.valueOf(repayShould.doubleValue() - repayAlready.doubleValue());
            overdue.setBackup1(s);
            overdue.setBackup2(String.valueOf(o.getOverdueInterest().doubleValue()));
            overdue.setOverdueInterest(new BigDecimal(rePay.getInterest()));
            overdue.setTotalOverdueMoney(new BigDecimal(rePay.getTotal()));
            overdue.setOverdueTime(o.getOverdueTime());
            overdue.setOverdueStatus(1);
            int i1 = overdueService.insertSelective(overdue);
        }

    }
}
